namespace PMS.BLL
{
    using PMS.DBUtility;
    using PMS.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class UserTypeBLL
    {
        public int Add(UserType model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into UserType(");
            builder.Append("TypeName,IsLeader");
            builder.Append(") values (");
            builder.Append("@TypeName,@IsLeader");
            builder.Append(") ");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@TypeName", model.TypeName), new SqlParameter("@IsLeader", model.IsLeader) };
            object single = DBHelper.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int TypeID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from UserType ");
            builder.Append(" where TypeID=@TypeID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@TypeID", SqlDbType.Int, 4) };
            cmdParms[0].Value = TypeID;
            return (DBHelper.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string TypeIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from UserType ");
            builder.Append(" where ID in (" + TypeIDlist + ")  ");
            return (DBHelper.ExecuteSql(builder.ToString()) > 0);
        }

        public List<UserTypeCount> GetCountList(int OrganID)
        {
            return new List<UserTypeCount>();
        }

        public List<UserType> GetList()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * ");
            builder.Append(" FROM UserType ");
            DataTable table = DBHelper.Query(builder.ToString()).Tables[0];
            UserType item = null;
            List<UserType> list = null;
            if (table.Rows.Count > 0)
            {
                list = new List<UserType>();
                foreach (DataRow row in table.Rows)
                {
                    item = new UserType();
                    if (row["TypeID"].ToString() != "")
                    {
                        item.TypeID = int.Parse(row["TypeID"].ToString());
                    }
                    item.TypeName = row["TypeName"].ToString();
                    if (row["IsLeader"].ToString() != "")
                    {
                        item.IsLeader = int.Parse(row["IsLeader"].ToString());
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public UserType GetModel(int TypeID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select TypeID, TypeName, IsLeader  ");
            builder.Append("  from UserType ");
            builder.Append(" where TypeID=@TypeID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@TypeID", SqlDbType.Int, 4) };
            cmdParms[0].Value = TypeID;
            UserType type = new UserType();
            DataSet set = DBHelper.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count > 0)
            {
                DataRow row = set.Tables[0].Rows[0];
                if (row["TypeID"].ToString() != "")
                {
                    type.TypeID = int.Parse(row["TypeID"].ToString());
                }
                type.TypeName = row["TypeName"].ToString();
                if (row["IsLeader"].ToString() != "")
                {
                    type.IsLeader = int.Parse(row["IsLeader"].ToString());
                }
                return type;
            }
            return null;
        }

        public bool Update(UserType model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update UserType set ");
            builder.Append(" TypeName = @TypeName , ");
            builder.Append(" IsLeader = @IsLeader  ");
            builder.Append(" where TypeID=@TypeID ");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@TypeID", model.TypeID), new SqlParameter("@TypeName", model.TypeName), new SqlParameter("@IsLeader", model.IsLeader) };
            return (DBHelper.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

